# -*- coding: utf-8 -*-

import warnings

import pandas as pd
from sklearn import preprocessing

from ..base import CommonFunction


class DataDiscretize(CommonFunction):
    def one_hot_encoding(self, handle_unknown='error', text_columns=None):
        if text_columns is not None:
            for idx, data in self._data.loc[:, text_columns].iteritems():
                self._data[idx] = preprocessing.LabelEncoder().fit_transform(self._data[idx])
        encoder = preprocessing.OneHotEncoder(handle_unknown=handle_unknown, categories='auto', sparse=False)
        self._data = pd.DataFrame(encoder.fit_transform(self._data))
        return encoder

    def binarize(self, threshold=0):
        encoder = preprocessing.Binarizer(threshold=threshold)
        self._data = pd.DataFrame(encoder.fit_transform(self._data, threshold), columns=self._data.columns.values)
        return encoder

    def bin_cut(self, index, method='freq', bin_number=1):
        if method == 'freq':
            self._data.index = pd.qcut(self._data.index, q=bin_number)
        elif method == 'isometric':
            self._data.index = pd.cut(self._data.index, bins=bin_number)
        else:
            msg = 'method shouldn\'t be' + str(method)
            raise RuntimeError(msg)


if __name__ == '__main__':
    data = pd.DataFrame([[1, 2, 3, 4, 5, 6],
                         [9, 8, 7, 6, 5, 4]], columns=list('ABCDEF'))
    handler = DataDiscretize(data)
